package com.justgame.project.generator.verify.impl;

import com.justgame.project.generator.schema.Entity;
import com.justgame.project.generator.schema.Field;
import com.justgame.project.generator.verify.IEntityValidation;
import org.springframework.stereotype.Component;
import org.springframework.util.Assert;

import java.util.List;
import java.util.stream.Collectors;

@Component
public class SimpleEntityValidation implements IEntityValidation {

    @Override
    public void valid(Entity entity) {
        Assert.hasLength(entity.getName(),String.format("在构建 %s 类时，未找到相关'业务实体名'",entity.getClass().getSimpleName()));
        Assert.hasLength(entity.getModuleName(),String.format("在构建 %s 类时，未找到相关'模块'",entity.getClass().getSimpleName()));
        Assert.isTrue(entity.getName().matches("[a-zA-Z0-9]+"),String.format("在构建 %s 实体时：实体名中不能包含特殊字符",entity.getName()));
        List<Field> fields = entity.getFields();
        List<String> fieldNames = fields.stream().map(Field::getName).distinct().collect(Collectors.toList());
        Assert.notNull(fields,String.format("在构建 %s 实体时：构建字段不能为空",entity.getName()));
        Assert.isTrue(fieldNames.size() == fields.size(),String.format("在构建 %s 实体时：不允许设置重复字段",entity.getName()));
    }

    @Override
    public int sort() {
        return 0;
    }
}
